package org.vincen.client5;

import java.util.concurrent.ExecutionException;

import javax.xml.ws.Response;

import org.apache.hello_world_async_soap_http.GreeterAsync;
import org.apache.hello_world_async_soap_http.SOAPService;
import org.apache.hello_world_async_soap_http.types.GreetMeSometimeResponse;

public class PollingClient {

	public static void main(String[] args) throws InterruptedException {

		SOAPService service = new SOAPService();
		GreeterAsync greet = service.getSoapPort();

		Response<GreetMeSometimeResponse> greetMeSometimeResp = greet
				.greetMeSometimeAsync(System.getProperty("user.name"));
		/*
		 * Non-blocking polling
		 * 尝试获得结果之前，调用非阻塞方法Response<T>.isDone()来检查响应消息是否到达
		 */
		while (!greetMeSometimeResp.isDone()) {
			Thread.sleep(100);
		}

		/*
		 * 如果没有前面isDone的检测，此处就退化为阻塞式轮询 Blocking polling
		 * 在get方法中，可以设置阻塞时长
		 * sayHiToUserResponseResp.get(5L,java.util.concurrent.TimeUnit.SECONDS);  
		 */
		try {
			GreetMeSometimeResponse reply = greetMeSometimeResp.get();
			System.out.println(reply.getResponseType());
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

	}

}
